package com.winning.joiner.magiccube.common.utils;

import java.util.Arrays;
import java.util.List;

public class FieldNameUtils {
	

    /** 下划线 */
    private static final char SEPARATOR = '_';
    
    /**
     * 驼峰首字符小写
     */
    public static String uncapitalize(String str)
    {
        if (str == null ||  str.length() == 0)
        {
            return str;
        }
        return new StringBuilder(str.substring(0, 1).toLowerCase()).append(str.substring(1)).toString();
    }
	
	 /**
     * 驼峰命名转下划线
     */
    public static String toUnderScoreCase(String s)
    {
        if (s == null)
        {
            return null;
        }
        StringBuilder sb = new StringBuilder();
        boolean upperCase = false;
        for (int i = 0; i < s.length(); i++)
        {
            char c = s.charAt(i);

            boolean nextUpperCase = true;

            if (i < (s.length() - 1))
            {
                nextUpperCase = Character.isUpperCase(s.charAt(i + 1));
            }

            if ((i > 0) && Character.isUpperCase(c))
            {
                if (!upperCase || !nextUpperCase)
                {
                    sb.append(SEPARATOR);
                }
                upperCase = true;
            }
            else
            {
                upperCase = false;
            }

            sb.append(Character.toLowerCase(c));
        }

        return sb.toString();
    }
    
    /**
     * 将下划线大写方式命名的字符串转换为驼峰式。如果转换前的下划线大写方式命名的字符串为空，则返回空字符串。 例如：HELLO_WORLD->HelloWorld
     * 下划线转驼峰
     * @param name 转换前的下划线大写方式命名的字符串
     * @return 转换后的驼峰式命名的字符串
     */
    public static String convertToCamelCase(String name)
    {
        StringBuilder result = new StringBuilder();
        // 快速检查
        if (name == null || name.isEmpty())
        {
            // 没必要转换
            return "";
        }
        else if (!name.contains("_"))
        {
            // 不含下划线，仅将首字母大写
            return name.substring(0, 1).toUpperCase() + name.substring(1).toLowerCase();
        }
        // 用下划线将原始字符串分割
        String[] camels = name.split("_");
        for (String camel : camels)
        {
            // 跳过原始字符串中开头、结尾的下换线或双重下划线
            if (camel.isEmpty())
            {
                continue;
            }
            // 首字母大写
            result.append(camel.substring(0, 1).toUpperCase());
            result.append(camel.substring(1).toLowerCase());
        }
        return result.toString();
    }
    
    
    public static void main(String[] args) {
        List<String> stringList = Arrays.asList("Doc_ID"
                , "Task_ID"
                , "MdTrtSN"
                , "PoolArea"
                , "PoolArea_name"
                , "MedIns_Code"
                , "MedIns_Lv"
                , "MedIns_Type"
                , "Patn_ID"
                , "Patn_Name"
                , "Gend"
                , "Patn_IDNum"
                , "Patn_SSCNO"
                , "Med_Type"
                , "Adm_Time"
                , "Dscg_Time"
                , "Setl_Time"
                , "Adm_Dise_codg"
                , "Adm_Dise_Name"
                , "Dscg_MainDise_Codg"
                , "Dscg_MainDise_Name"
                , "Dscg_Dept_Codg"
                , "Dscg_Dept_Name"
                , "Dr_Codg"
                , "MedFee_SumAmt"
                , "Chk_Prog"
                , "Vola_Flag"
                , "Doc_Stas"
                , "Crt_Node_ID"
                , "Cnfm_Vola_Amt"
                , "Vola_Det_Amt"
                , "Revise_Amt");
        for (String s : stringList) {
            String s1 = convertToCamelCase(s);
            System.out.println(uncapitalize(s1));
        }
        System.out.println("-----------");
        List<String> stringList2 = Arrays.asList("VARCHAR" , "VARCHAR" , "VARCHAR" , "VARCHAR" , "VARCHAR" , "VARCHAR" , "VARCHAR" , "VARCHAR" , "VARCHAR" , "VARCHAR" , "VARCHAR" , "VARCHAR" , "VARCHAR" , "VARCHAR" , "DATETIME" , "DATETIME" , "DATETIME" , "VARCHAR" , "VARCHAR" , "VARCHAR" , "VARCHAR" , "VARCHAR" , "VARCHAR" , "VARCHAR" , "NUMBER" , "NUMBER" , "VARCHAR" , "VARCHAR" , "VARCHAR" , "NUMBER" , "NUMBER" , "NUMBER");
        for (String s : stringList2) {
            System.out.println(toJAVAType(s));
        }
    }

	private static  String toJAVAType(String dbType){
        if ("VARCHAR".equals(dbType)){
            return "String";
        }else if("NUMBER".equals(dbType)){
            return "BigDecimal";
        }else if("DATETIME".equals(dbType)){
            return "Date";
        }
        return "Integer";
    }

}
